package org.onionframework.persistence.dao.dynamic;

import java.io.StringWriter;
import java.util.Map;

import org.onionframework.core.util.Exceptions;

import freemarker.template.Template;

/**
 * 表达一个语句模板的对象
 * 
 * @author allan
 *
 */
public class StatementTemplate {

	/**
	 * 类型，可能是SQL语句模板，也可能是HQL语句模板
	 */
	private StatementType type;
	
	private Template template;
	
	public StatementTemplate(StatementType type, Template template){
		this.type = type;
		this.template = template;
	}

	public StatementType getType() {
		return type;
	}

	public Template getTemplate() {
		return template;
	}
	
	/**
	 * 根据模板和参数，调用freemarket的逻辑，生成最终的语句
	 */
	public String processTemplate(Map<String, Object> parameters){  
        StringWriter stringWriter = new StringWriter();  
        try {  
            this.template.process(parameters, stringWriter);  
        } catch (Exception e) {  
            throw Exceptions.unchecked(e);
        }
        return stringWriter.toString().trim();  
    }
	
}
